
package com.borqs.music.ui.online;

import static com.borqs.music.tasks.GetSingerListTask.DEFAULT_COUNT;
import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.STATUS_FAILED;
import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.STATUS_LOADING;
import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.STATUS_MORE;
import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.STATUS_NORMAL;
import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.TYPE_NORMAL;
import static com.borqs.music.ui.adapter.OnlineSingerListAdapter.TYPE_STATUS;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;

import com.borqs.music.database.CacheUtils.SingerCache;
import com.borqs.music.executor.MusicExecutor;
import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskAdapter;
import com.borqs.music.executor.TaskListener;
import com.borqs.music.model.SingerInfo;
import com.borqs.music.tasks.GetSingerListTask;
import com.borqs.music.ui.adapter.OnlineSingerListAdapter;

public class OnlineSingerListActivity extends OnlineBaseActivity {
    private final int LOAD_CONTENT_LIST_START = 1;
    private final int LOAD_CONTENT_LIST_COMPLETE = 2;
    private final int LOAD_CONTENT_LIST_FAILED = 3;

    private int mPageNumber;
    private int mArea;
    private OnlineSingerListAdapter mAdapter;
    private List<SingerInfo> mSingerInfoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String titleResId = intent.getStringExtra("title");
        setTitle(titleResId);
        mArea = getIntent().getIntExtra("area", 0);
        //TODO currently disable load from cache.
        // mSingerInfoList = SingerCache.get(this, mArea);
        mSingerInfoList = new ArrayList<SingerInfo>();
        mAdapter = new OnlineSingerListAdapter(this, mSingerInfoList, getListView());
        setListAdapter(mAdapter);
        handler.sendEmptyMessage(LOAD_CONTENT_LIST_START);
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case LOAD_CONTENT_LIST_START:
                    mAdapter.setStatus(STATUS_LOADING);
                    mAdapter.notifyDataSetChanged();
                    executeTask();
                    break;
                case LOAD_CONTENT_LIST_COMPLETE:
                    @SuppressWarnings("unchecked")
                    List<SingerInfo> singers = (List<SingerInfo>) msg.obj;
                    if (mPageNumber == 0) {
                        mSingerInfoList.clear(); // clear the old cache
                        Context context = OnlineSingerListActivity.this;
                        SingerCache.put(context, mArea, singers);
                    }
                    mSingerInfoList.addAll(singers);

                    if (singers.size() < DEFAULT_COUNT) {
                        mAdapter.setStatus(STATUS_NORMAL);
                    } else {
                        mAdapter.setStatus(STATUS_MORE);
                    }
                    mAdapter.notifyDataSetChanged();

                    mPageNumber++;
                    break;
                case LOAD_CONTENT_LIST_FAILED:
                    mAdapter.setStatus(STATUS_FAILED);
                    mAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    private void executeTask() {

        GetSingerListTask task = new GetSingerListTask(getSingerListListener, mTicket, mPageNumber,
                mArea);
        MusicExecutor exec = MusicExecutor.getInstance();
        exec.execute(task);
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        if (!checkActiveNetworkStatus()) {
            return;
        }

        int type = mAdapter.getItemViewType(position);
        if (type == TYPE_STATUS) {
            if (mAdapter.getStatus() != STATUS_LOADING) {
                handler.sendEmptyMessage(LOAD_CONTENT_LIST_START);
            }
            return;
        }

        if (type == TYPE_NORMAL) {
            Intent intent = new Intent();
            intent.setClass(this, OnlineTrackBrowserActivity.class);
            intent.putExtra(OnlineTrackBrowserActivity.KEY_TYPE,
                    OnlineTrackBrowserActivity.TYPE_SINGER);
            intent.putExtra(OnlineTrackBrowserActivity.KEY_SINGER,
                    mSingerInfoList.get(position).name);
            startActivity(intent);
            return;
        }
    };

    TaskListener getSingerListListener = new TaskAdapter() {

        public void onDone(Task task) {
            Message msg = handler.obtainMessage(LOAD_CONTENT_LIST_COMPLETE);
            msg.obj = task.getResult();
            handler.sendMessage(msg);
        };

        public void onFail(Task task) {
            handler.sendEmptyMessage(LOAD_CONTENT_LIST_FAILED);
        };
    };
}
